package com.yuji.aacar.net.crash;

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.yuji.aacar.core.cores.utils.CoreDeviceUtil;
import com.yuji.aacar.core.cores.utils.CoreFileUtil;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.net.util.ConnectUtil;

import java.util.HashMap;

/**
 * <li>Package:com.ttp.core.cores.net
 * <li>Author: lining
 * <li>Date: 2017/5/27
 * <li>Description: 崩溃日志、服务器日志上传任务
 * boss,dealer,customer,checker 目前只有这四个AppName
 */

public class CrashReportTask extends AsyncTask{

    private String mAppName;//应用名称
    private Context mContext;
    private String mUrl;//服务地址
    private HashMap<String, String> maps;
    private String mHost;//空host地址
    private String isServer = "";//是否是服务器类型打点

    /**
     *
     * @param context 上下文
     * @param appName 请求应用
     * @param url   请求url
     * @param host host地址
     */
    public CrashReportTask(Context context, String appName, String url, String host) {
        this.mContext = context;
        this.mUrl = url;
        this.mAppName = appName;
        this.mHost = host;
        if (TextUtils.isEmpty(url)){
            this.mUrl = "http://api.ttpai.cn/logappapi/add";
        }
        if (TextUtils.isEmpty(host)){
            this.mHost = "logapp.ttpai.cn";
        }
    }

    /**
     * 构造方法
     * @param context 上下文
     * @param appName 应用名称
     * @param url 请求url
     * @param host 请求host
     * @param isServer 打点类型是不是 服务器类型
     */
    public CrashReportTask(Context context, String appName, String url, String host, String isServer) {
        this.mContext = context;
        this.mUrl = url;
        this.mAppName = appName;
        this.mHost = host;
        if (TextUtils.isEmpty(url)){
            this.mUrl = "http://api.ttpai.cn/logappapi/add";
        }
        if (TextUtils.isEmpty(host)){
            this.mHost = "logapp.ttpai.cn";
        }
        this.isServer = isServer;
    }

    @Override
    protected Object doInBackground(Object[] objects) {
        if(!LogUtil.getLogType() || LogUtil.getExceptionType()){
            if (null != CoreFileUtil.LOG_FILE_FOR_CRASH_REPORT){
                LogUtil.e("--crash file name--",CoreFileUtil.LOG_FILE_FOR_CRASH_REPORT.getName());
                LogUtil.e("--crash file path--",CoreFileUtil.LOG_FILE_FOR_CRASH_REPORT.getAbsolutePath());
                if (CoreFileUtil.LOG_FILE_FOR_CRASH_REPORT.exists()){
                    String contents = CoreFileUtil.readFileByLines(CoreFileUtil.LOG_FILE_FOR_CRASH_REPORT);
                    //                LogUtil.e("contents", contents);
                    //                ConnectUtil.testCrash(contents);
                    maps = new HashMap<String, String>();
                    maps.put("app", mAppName);
                    maps.put("isServer", isServer);
                    maps.put("version", CoreDeviceUtil.getVersionAll(mContext));
                    maps.put("type", "Android");
                    maps.put("content", contents);

                    if (ConnectUtil.postForHostUrl(mUrl,mHost,maps)){
                        boolean isDelete = CoreFileUtil.deleteFile(CoreFileUtil.LOG_FILE_FOR_CRASH_REPORT);
                        LogUtil.e("--crash file deleted--",isDelete);
                    } else {
                        LogUtil.e("--crash file not deleted--","");
                    }
                }
            }

            if (null != CoreFileUtil.LOG_FILE_FOR_SERVER_REPORT){
                LogUtil.e("--server file name--",CoreFileUtil.LOG_FILE_FOR_SERVER_REPORT.getName());
                LogUtil.e("--server file path--",CoreFileUtil.LOG_FILE_FOR_SERVER_REPORT.getAbsolutePath());
                if (CoreFileUtil.LOG_FILE_FOR_SERVER_REPORT.exists()){
                    String contents = CoreFileUtil.readFileByLines(CoreFileUtil.LOG_FILE_FOR_SERVER_REPORT);
                    //                LogUtil.e("contents", contents);
                    //                ConnectUtil.testCrash(contents);
                    maps = new HashMap<String, String>();
                    maps.put("app", mAppName);
                    maps.put("isServer", "1");
                    maps.put("version", CoreDeviceUtil.getVersionAll(mContext));
                    maps.put("type", "Android");
                    maps.put("content", contents);

                    if (ConnectUtil.postForHostUrl(mUrl,mHost,maps)){
                        boolean isDelete = CoreFileUtil.deleteFile(CoreFileUtil.LOG_FILE_FOR_SERVER_REPORT);
                        LogUtil.e("--server file deleted--",isDelete);
                    } else {
                        LogUtil.e("--server file not deleted--","");
                    }
                }
            }
        }
        return null;
    }
}
